package cn.netuo.bumbersoll;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;

import com.dueeeke.videoplayer.player.PlayerConfig;
import com.rooten.frame.AppActivity;

import cn.netuo.bumbersoll.ijk.SunController;
import cn.netuo.bumbersoll.ijk.SunIjkVideoView;
import lib.grasp.util.BarUtils;
import lib.grasp.widget.ProhibitViewGroup;

/**
 * Created by JS_grasp on 2019/3/18.
 */
public class MainActivity extends AppActivity<MainPtr> {

    private View            mView;
    private SunIjkVideoView mIjkVideoView;
    private SunController   mController;

    /** 禁用下拉菜单辅助类 */
    private ProhibitViewGroup   mProhibitViewGroup;

    @Override
    public MainPtr onBindPresenter() {
        return new MainPtr(this);   // 生成Presenter的同时, 将自身(View)传进去
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if(App.getApp().getFileDownloadMgr() == null) App.getApp().doInit();  // 将APP设置成功桌面之后, App只会初始化一次, 但是成员变量会丢失
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        mView = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
        installView(mView, 1);
        init();
    }

    @Override
    protected void initActionMenu(MenuItem item) {
        super.initActionMenu(item);
        mToolbar.setVisibility(View.GONE);
        BarUtils.setStatusBarVisibility(this, false);
        mToolbar.setNavigationIcon(null);
    }

    private void init(){
        initView();
        initIjk();
        getPresenter().init();
    }

    /** 初始化布局 */
    private void initView(){
        mIjkVideoView   = mView.findViewById(R.id.player);
    }

    private void initIjk(){
        mController = new SunController(this);
        mController.setSunIjkVideoView(mIjkVideoView);
        mIjkVideoView.setVideoController(mController); //设置控制器，如需定制可继承BaseVideoController
        mController.hide();
        mController.doLockAll();
        // 高级设置（可选，须在start()之前调用方可生效）
        PlayerConfig playerConfig = new PlayerConfig.Builder()
//                .autoRotate()           //启用重力感应自动进入/退出全屏功能
//                .enableMediaCodec()     //启动硬解码，启用后可能导致视频黑屏，音画不同步
                .usingSurfaceView()     //启用SurfaceView显示视频，不调用默认使用TextureView
                .savingProgress()       //保存播放进度
                .disableAudioFocus()    //关闭AudioFocusChange监听
//                .setLooping()           //循环播放当前正在播放的视频(开了这个就不会回调onCompletion)
                .build();
        mIjkVideoView.setPlayerConfig(playerConfig);
        mIjkVideoView.start();
        mIjkVideoView.retry();
    }

    /** 亮屏&开始播放 */
    boolean doStartPlay(){
        if(mIjkVideoView == null) return false;
        mView.postDelayed(() -> {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            mIjkVideoView.notifyDataChanged();
        }, 200);
        return true;
    }

    /** 停止播放&灭屏 */
    boolean doStopPlay(){
        if(mIjkVideoView == null) return false;
        mView.postDelayed(() -> {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            mIjkVideoView.doStopPlay();
        }, 200);
        return true;
    }


    /** mqtt连接状态发生变化 */
    void onConnStateChanged(boolean isConned) {
        if(mController == null) return;
        mController.onMqttConnectionChanged(isConned);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(mIjkVideoView != null)mIjkVideoView.pause();
        if(mProhibitViewGroup != null) BarUtils.allowStatusPanelDropDown(getPresenter().mWindowManager, mProhibitViewGroup);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(mIjkVideoView != null)mIjkVideoView.resume();
        if(mController != null){
            mController.hide();
            mController.doLockAll();
        }
        if(getPresenter().mWindowManager != null) mProhibitViewGroup = BarUtils.prohibitStatusPanelDropDown(this, getPresenter().mWindowManager);
    }

    @Override
    protected void onDestroy() {
        if(mIjkVideoView != null) mIjkVideoView.release();
        getPresenter().onDestroy();
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        if (!mIjkVideoView.onBackPressed()) {
            super.onBackPressed();
        }
    }

    boolean isPlaying(){
        return mIjkVideoView != null && mIjkVideoView.isPlaying();
    }

    public SunIjkVideoView getIjkVideoView() {
        return mIjkVideoView;
    }

    public View getView() {
        return mView;
    }
}
